<template>
    <div class="container">
        <div class="content_wrapper">
            <div class="bonus-wrap">
                <p class="subtitle">按优惠方式快速创建</p>
                <div class="card-wrap">
                    <div class="card-wrap__container">
                        <div class="item-bg coupon-cards-item">
                            <div>
                                <div class="title">满减</div>
                                <div class="desc">
                                    <p>例：100元减20元<br>便于合理控制活动成本</p>
                                </div>
                            </div>
                            <DialogForm isDrawer @okCallback="loadFilter" title="添加减免优惠活动" width="800px"
                                path="promotion/productActivity/Info" :params="{ act: 'add', promotion_type: 1 }">
                                <el-button type="primary">立即新建</el-button>
                            </DialogForm>
                        </div>
                        <div class="item-bg coupon-cards-item">
                            <div>
                                <div class="title">折扣</div>
                                <div class="desc">
                                    <p>例：100元打9折<br>提高店铺销量和客单价</p>
                                </div>
                            </div>
                            <DialogForm isDrawer @okCallback="loadFilter" title="添加折扣优惠活动" width="800px"
                                path="promotion/productActivity/Info" :params="{ act: 'add', promotion_type: 2 }">
                                <el-button type="primary">立即新建</el-button>
                            </DialogForm>
                        </div>
                        <div class="item-bg coupon-cards-item item-bg2">
                            <div>
                                <div class="title">赠品</div>
                                <div class="desc">
                                    <p>据会员等级可领的赠品，丰富会<br>员权益，凸显会员身份价值</p>
                                </div>
                            </div>
                            <DialogForm isDrawer @okCallback="loadFilter" title="添加赠品优惠活动" width="800px"
                                path="promotion/productActivity/Info" :params="{ act: 'add', promotion_type: 0 }">
                                <el-button type="primary">立即新建</el-button>
                            </DialogForm>
                        </div>
                    </div>
                </div>
            </div>
            <div class="lyecs-table-list-warp">
                <div class="list-table-tool lyecs-search-warp">
                    <el-form :model="filterParams">
                        <div class="advanced-search-warp list-table-tool-row">
                            <div class="simple-form-warp">
                                <div class="simple-form-field">
                                    <div class="form-group">
                                        <label class="control-label"><span>活动名称：</span></label>
                                        <div class="control-container">
                                            <el-input name="keyword" v-model="filterParams.keyword" placeholder="输入活动名称">
                                            </el-input>
                                        </div>
                                    </div>
                                </div>
                                <div class="simple-form-field">
                                    <div class="form-group">
                                        <label class="control-label"><span>是否显示：</span></label>
                                        <div class="control-container">
                                            <el-select v-model="filterParams.is_going" clearable>
                                                <el-option v-for="(item, key) in promotion_status" :value="key+ 1" :label="item" />
                                            </el-select>
                                        </div>
                                    </div>
                                </div>
                                <div class="simple-form-field">
                                    <el-button type="primary" @click="onSearchSubmit">搜索</el-button>
                                </div>
                            </div>
                        </div>
                    </el-form>
                </div>
                <div class="table-container">
                    <a-spin :spinning="loading">
                        <el-table :data="filterState" row-key="coupon_id" @selection-change="onSelectChange" :total="total"
                            @sort-change="onSortChange" :loading="loading">
                            <el-table-column type="selection" width="32" />
                            <el-table-column label="活动名称" prop="promotion_name" :width="150">
                                <template #default="{ row }">
                                    <div style="position: relative;">
                                        <div>{{ row.promotion_name || '--' }}</div>
                                    </div>
                                    <p class="order-tips-p">
                                        <span class="green">
                                            {{ row.promotion_type == 1 ? '减免' : row.promotion_type == 2 ? '折扣' : '赠品'}}
                                        </span>
                                    </p>
                                </template>
                            </el-table-column>
                            <el-table-column label="起始日期" prop="start_time" width="150" sortable="custom">
                            </el-table-column>
                            <el-table-column label="截止日期" prop="end_time" width="150" sortable="custom">
                            </el-table-column>
                            <el-table-column label="金额下限" prop="min_order_amount" sortable="custom">
                                <template #default="{ row }">
                                    <!-- <PopForm label="金额下限" type="input" :requestApi="updateProductActivityFiled" v-model:org-value="row.min_order_amount"
                                        :params="{ id: row.promotion_id, field: 'min_order_amount' }"> -->
                                        <div>{{ row.min_order_amount }}</div>
                                    <!-- </PopForm> -->
                                </template>
                            </el-table-column>
                            <el-table-column label="金额上限" prop="max_order_amount" sortable="custom">
                                <template #default="{ row }">
                                    <!-- <PopForm label="金额上限" type="input" :requestApi="updateProductActivityFiled" v-model:org-value="row.max_order_amount"
                                        :params="{ id: row.promotion_id, field: 'max_order_amount' }"> -->
                                        <div>{{ row.max_order_amount }}</div>
                                    <!-- </PopForm> -->
                                </template>
                            </el-table-column>
                            <el-table-column label="排序" prop="sort_order" sortable="custom">
                                <template #default="{ row }">
                                    <PopForm label="排序" type="input" :requestApi="updateProductActivityFiled" v-model:org-value="row.sort_order"
                                        :params="{ id: row.promotion_id, field: 'sort_order' }" extra="默认值为50，数值越小，排序越靠前">
                                        <div>{{ row.sort_order }}</div>
                                    </PopForm>
                                </template>
                            </el-table-column>
                            <el-table-column label="状态" prop="product_status"></el-table-column>
                            <el-table-column label="是否启用" prop="is_show" sortable="custom">
                                <template #default="{ row }">
                                    <Switch v-model:checked="row.is_available" :requestApi="updateProductActivityFiled"
                                        :params="{ id: row.promotion_id, field: 'is_available' }" />
                                </template>
                            </el-table-column>
                            <el-table-column label="操作" fixed="right">
                                <template #default="{ row }">
                                    <DialogForm isDrawer @okCallback="loadFilter" title="编辑优惠活动" width="800px"
                                        path="promotion/productActivity/Info" :params="{ act: 'detail', id: row.promotion_id }">
                                        <a class="btn-link">编辑</a>
                                    </DialogForm>
                                    <el-divider direction="vertical" />
                                    <DeleteRecord @afterDelete="loadFilter" :requestApi="delProductActivity"
                                        :params="{ id: row.promotion_id }">删除</DeleteRecord>
                                </template>
                            </el-table-column>
                            <template #empty>
                                <div class="empty-warp">
                                    <div v-if="!loading" class="empty-bg">暂无数据</div>
                                </div>
                            </template>
                        </el-table>
                    </a-spin>
                    <div class="pagination-con" v-if="total > 0">
                        <Pagination v-model:page="filterParams.page" v-model:size="filterParams.size" :total="total"
                            @callback="loadFilter" />
                    </div>
                </div>
                <div class="selected-action-warp" v-if="selectedIds.length > 0">
                    <div class="selected-action">
                        <el-space>
                            <span>已选择：<b>{{ selectedIds.length }}</b> 项</span>
                            <el-popconfirm title="您确认要批量删除所选数据吗？" @confirm="onBatchSubmit('del')">
                                <template #reference><el-button>批量删除</el-button></template>
                            </el-popconfirm>
                        </el-space>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import '@/style/css/list.less'
import { DialogForm } from '@/components/dialog'
import { PopForm } from '@/components/pop-form'
import { ref, reactive, onMounted } from 'vue';
import { DeleteRecord, Switch, Pagination } from '@/components/list';
import { Image } from '@/components/image';
import { message } from 'ant-design-vue'
import { useConfigStore } from "@/store/config";
import { ProductActivityFilterState, ProductActivityFilterParams } from '@/types/promotion/productActivity.d';
import { getProductActivityList, batchSubmit, updateProductActivityFiled, delProductActivity, getProductActivityConfig } from "@/api/promotion/productActivity";
import { SelectShop } from '@/components/select'
const config:any = useConfigStore();
// 基本参数定义
const filterState = ref<ProductActivityFilterState[]>();
const loading = ref<boolean>(true);
const total = ref<number>(0);
const selectedIds = ref<number[]>([]);
const filterParams = reactive<ProductActivityFilterParams>({
    page: 1,
    size: config.get('page_size'),
    sort_field: '',
    sort_order: '',
    keyword: ''
});
const promotion_status = ref<string[]>([])
// 获取列表的查询结果
const loadFilter = async () => {
    loading.value = true;
    try {
        const result = await getProductActivityList({ ...filterParams });
        filterState.value = result.filter_result;
        total.value = result.total;
        const config = await getProductActivityConfig()
        promotion_status.value = config.promotion_status
    } catch (error: any) {
        message.error(error.message);
    } finally {
        loading.value = false;
    }
}
onMounted(() => {
    loadFilter();
});

// 参数查询
const onSearchSubmit = () => {
    loadFilter()
};
// 修改排序
const onSortChange = ({ prop, order }: { prop: string; order?: string }) => {
    filterParams.sort_field = prop;
    filterParams.sort_order = order == 'ascending' ? 'asc' : order == 'descending' ? 'desc' : '';
    loadFilter();
};

// 批量操作
const onBatchSubmit = async (action: string) => {
    try {
        const result = await batchSubmit(action, { ids: selectedIds.value });
        message.success(result.message);
        loadFilter();
    } catch (error: any) {
        message.error(error.message);
    }
};
// 多选操作
const onSelectChange = (e: ProductActivityFilterState[]) => {
    selectedIds.value = e.map((item) => item.promotion_id);
};
</script>
<style>
/*优惠券列表类型选择*/
.bonus-wrap .card-wrap {
    display: -ms-flexbox;
    display: flex;
    margin-bottom: 24px;
    -ms-flex-pack: start;
    justify-content: flex-start;
    overflow: hidden;
    z-index: -1
}

.bonus-wrap .card-wrap__container {
    transition: transform .6s ease-in-out;
    display: -ms-flexbox;
    display: flex
}

.bonus-wrap .item-bg {
    position: relative;
    white-space: nowrap;
    padding: 20px 27px 17px;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-direction: column;
    flex-direction: column;
    background: rgba(51, 136, 255, .07);
    margin-right: 15px;
    text-align: center;
    -ms-flex-align: center;
    align-items: center;
    -ms-flex-pack: justify;
    justify-content: space-between
}

.bonus-wrap .coupon-cards-item {
    min-width: 166px;
    width: 170px
}

.bonus-wrap .coupon-cards-item .title {
    font-size: 16px;
    color: #333
}

.bonus-wrap .coupon-cards-item .desc {
    padding-top: 10px;
    line-height: 18px;
    font-size: 12px;
    color: #666
}

.bonus-wrap .coupon-cards-item button {
    margin-top: 15px;
    padding: 0 16px;
}

.bonus-wrap .subtitle {
    font-weight: 500;
    font-size: 14px;
    color: #323233;
    line-height: 20px;
    margin-bottom: 16px
}

.bonus-wrap .item-bg2 {
    background: #fff;
    border: 1px solid #ebedf0;
    box-sizing: border-box;
    width: 223px !important
}</style>